package me.firstapp.repository.weixin.product.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.product.Product;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.weixin.product.ProductRepository;

@Repository
public class ProductRepositoryImpl extends EntityRepositoryImpl<Product> implements ProductRepository {

	@Override
	protected Class<Product> getPersistentClass() {
		return Product.class;
	}

	@Override
	public Page<Product> findProductPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		StringBuilder sql = new StringBuilder(" FROM Product bean where 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					if ("productCategoryId".equals(key)) {
						String _value = (String) value;
						Long categoryId = Long.valueOf(_value);
						sql.append(" AND bean.productCategoryId =:productCategoryId ");
						parameters.put("productCategoryId", categoryId);
					} else if ("status".equals(key)) {
						String _value = (String) value;
						Integer status = Integer.valueOf(_value);
						sql.append(" AND bean.status =:status ");
						parameters.put("status", status);
					} else if ("categoryName".equals(key)) {
						sql.append(" AND bean.categoryName LIKE :categoryName ");
						parameters.put("categoryName", "%" + value + "%");
					} else {
						sql.append(" AND bean." + key + " LIKE :" + key + " ");
						parameters.put(key, "%" + value + "%");
					}
				}
			}
		}

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(Product.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public List<Product> findByCategoryId(Integer pageNo, Integer pageSize, Long productCategoryId) {
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.productCategoryId = :productCategoryId ");
		parameters.put("productCategoryId", productCategoryId);

		sql.append(" AND bean.status = :status ");
		parameters.put("status", Product.STATUS_ON);

		sql.append(" ORDER BY bean.saleCounts DESC");

		return super.queryForPageList(Product.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public List<Product> findByTuiJian() {
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.tuiJian = :tuiJian ");
		parameters.put("tuiJian", Product.TUIJIAN_ON);

		sql.append(" AND bean.status = :status ");
		parameters.put("status", Product.STATUS_ON);

		return super.query(Product.class, sql.toString(), parameters);
	}

	@Override
	public List<Product> findByKeyword(Integer pageNo, Integer pageSize, String keyword) {
		StringBuilder sql = new StringBuilder(" FROM Product bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.name LIKE :name ");
		parameters.put("name", "%" + keyword + "%");

		sql.append(" AND bean.status = :status ");
		parameters.put("status", Product.STATUS_ON);

		return super.queryForPageList(Product.class, sql.toString(), parameters, pageNo, pageSize);
	}

}
